package com.info.utils;

import java.io.UnsupportedEncodingException;
import java.math.BigDecimal;
import java.net.URLDecoder;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
/**
 * @author HAHA
 * 
 */
public class StringTools {
	
	//转化字符串为十六进制编码   
		public static String toHexString(String s) {  
		   String str = "";  
		   for (int i = 0; i < s.length(); i++) {  
		    int ch = (int) s.charAt(i);  
		    String s4 = Integer.toHexString(ch);  
		    str = str + s4;  
		   }  
		   return str;  
		}  
		
		// 转化十六进制编码为字符串
		public static String toStringHex2(String s) {
			byte[] baKeyword = new byte[s.length() / 2];
			for (int i = 0; i < baKeyword.length; i++) {
				try {
					baKeyword[i] = (byte) (0xff & Integer.parseInt(
							s.substring(i * 2, i * 2 + 2), 16));
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
			try {
				s = new String(baKeyword, "utf-8");// UTF-16le:Not
			} catch (Exception e1) {
				e1.printStackTrace();
			}
			return s;
		}
		
		public static String leftAdding(String str,String info,int length){
			int size=length-str.length();
			StringBuffer result=new StringBuffer("");
			for(int i=0;i<size;i++){
				result.append(info);
			}
			result.append(str);
			return result.toString();
		}
	
	public static String ISO2GBK(String text) {
		String result = "";
		try {
			result = new String(text.getBytes("ISO-8859-1"), "GBK");
		} catch (UnsupportedEncodingException ex) {
			result = ex.toString();
		}
		return result;
	}

	public static String GBKISO(String text) {
		String result = "";
		try {
			result = new String(text.getBytes("GBK"), "ISO-8859-1");
		} catch (UnsupportedEncodingException ex) {
			ex.printStackTrace();
		}
		return result;
	}

	private static String Utf8URLencode(String text) {
		StringBuffer result = new StringBuffer();

		for (int i = 0; i < text.length(); i++) {
			char c = text.charAt(i);
			if ((c >= 0) && (c <= 'ÿ')) {
				result.append(c);
			} else {
				byte[] b = new byte[0];
				try {
					b = Character.toString(c).getBytes("UTF-8");
				} catch (Exception localException) {
				}
				for (int j = 0; j < b.length; j++) {
					int k = b[j];
					if (k < 0)
						k += 256;
					result.append("%" + Integer.toHexString(k).toUpperCase());
				}
			}

		}

		return result.toString();
	}

	private static String Utf8URLdecode(String text) {
		String result = "";
		int p = 0;

		if ((text != null) && (text.length() > 0)) {
			text = text.toLowerCase();
			p = text.indexOf("%e");
			if (p == -1)
				return text;

			while (p != -1) {
				result = result + text.substring(0, p);
				text = text.substring(p, text.length());
				if ((text == "") || (text.length() < 9))
					return result;

				result = result + CodeToWord(text.substring(0, 9));
				text = text.substring(9, text.length());
				p = text.indexOf("%e");
			}

		}

		return result + text;
	}

	private static String CodeToWord(String text) {
		String result;
		if (Utf8codeCheck(text)) {
			byte[] code = new byte[3];
			code[0] = (byte) (Integer.parseInt(text.substring(1, 3), 16) - 256);
			code[1] = (byte) (Integer.parseInt(text.substring(4, 6), 16) - 256);
			code[2] = (byte) (Integer.parseInt(text.substring(7, 9), 16) - 256);
			try {
				result = new String(code, "UTF-8");
			} catch (UnsupportedEncodingException ex) {
				result = null;
			}
		} else {
			result = text;
		}

		return result;
	}

	private static boolean Utf8codeCheck(String text) {
		String sign = "";
		if (text.startsWith("%e")) {
			int i = 0;
			for (int p = 0; p != -1; i++) {
				p = text.indexOf("%", p);
				if (p != -1)
					p++;
				sign = sign + p;
			}
		}
		return sign.equals("147-1");
	}

	public static boolean isUtf8Url(String text) {
		text = text.toLowerCase();
		int p = text.indexOf("%");
		if ((p != -1) && (text.length() - p > 9)) {
			text = text.substring(p, p + 9);
		}
		return Utf8codeCheck(text);
	}

	public static String UTF8Decode(String text) {
		if (isUtf8Url(text))
			return Utf8URLdecode(text);
		return URLDecoder.decode(text);
	}

	public static String UTF8encode(String text) {
		return Utf8URLencode(text);
	}
	/**
	 * 把一个str转换成Date
	 * 
	 * @param str
	 * @return
	 */
	public static Date getDateByStr(String str) {

		int dotIndex = str.indexOf(".");
		if (dotIndex != -1)
			str = str.substring(0, str.indexOf("."));

		SimpleDateFormat myFmt = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		try {
			Date newdate = myFmt.parse(str);
			return newdate;
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}

	/**
	 * 把一个date格式化成string
	 * 
	 * @param date
	 * @return
	 */
	public static String getStrByDate(Date date) {
		SimpleDateFormat myFmt = new java.text.SimpleDateFormat(
				"yyyy-MM-dd HH:mm:ss");
		return myFmt.format(date);
	}
	
	public static BigDecimal getBigDecimalSum(String bd1,String bd2){
		BigDecimal _bd1=new BigDecimal(bd1);
		BigDecimal _bd2=new BigDecimal(bd2);
		return _bd1.add(_bd2);
	}
	
	public static void main(String args[]){
		System.out.println(StringTools.toHexString("company0010854.0900E3414.3940N9police0010855.1160E3413.6860S999home0010857.5100E3418.3563S"));
		Calendar nowtime=Calendar.getInstance();
		nowtime.add(Calendar.MINUTE, 5);
		System.out.println(toHexString("电"));
		//System.out.println(Integer.parseInt("037a", 16));
		//System.out.println("123456".substring(2, 4));
		System.out.println(new BigDecimal(0).compareTo(new BigDecimal(1)));
	}
}